package liming.tool.handle.net.tu;

import liming.tool.handle.net.root.RootSocket;
import liming.tool.handle.net.tcp.socket.TcpReceiveMap;
import liming.tool.handle.net.tcp.socket.TcpServetSocket;
import liming.tool.handle.net.tcp.socket.once.OnceTcpClientSocket;
import liming.tool.handle.net.udp.socket.UdpReceiveMap;
import liming.tool.handle.net.udp.socket.UdpServerSocket;

import java.io.IOException;
import java.net.InetAddress;

public abstract class TUServerSocket extends RootSocket {
    private final TcpServetSocket tcpServetSocket;
    private final UdpServerSocket udpServerSocket;

    /**
     * 默认构造函数，在网络通信中，套接字信息都是一个必要的参数
     */
    public TUServerSocket(InetAddress inetAddress, int port) throws IOException {
        super(inetAddress, port);
        tcpServetSocket = new TcpServetSocket(port) {

            @Override
            public boolean doWork(TcpReceiveMap request, TcpReceiveMap respond) throws Exception {
                return doTcpWork(request, respond);
            }
        };
        udpServerSocket = new UdpServerSocket(inetAddress, port) {
            @Override
            public boolean doWork(UdpReceiveMap request, UdpReceiveMap respond) throws Exception {
                return doUdpWork(request, respond);
            }
        };
    }

    public void send(UdpReceiveMap udpReceiveMap, InetAddress inetAddress, int port) throws IOException, ClassNotFoundException {
        udpServerSocket.send(udpReceiveMap, inetAddress, port);
    }

    public TcpReceiveMap send(TcpReceiveMap tcpReceiveMap, InetAddress inetAddress, int port, long time, boolean always) throws IOException, ClassNotFoundException {
        return new OnceTcpClientSocket(inetAddress, port, tcpReceiveMap).getRespond(time, always);
    }

    @Override
    public void close() throws IOException {
        udpServerSocket.close();
        tcpServetSocket.close();
    }

    public abstract boolean doTcpWork(TcpReceiveMap request, TcpReceiveMap respond) throws Exception;

    public abstract boolean doUdpWork(UdpReceiveMap request, UdpReceiveMap respond) throws Exception;

}
